package com.jzxiang.pickerview.sample.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;

import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.jzxiang.pickerview.sample.ResourceTable;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * MainAbilitySlice
 *
 * @since 2021-07-16
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener, OnDateSetListener {
    private TimePickerDialog mDialogAll;
    private TimePickerDialog mDialogYearMonth;
    private TimePickerDialog mDialogYearMonthDay;
    private TimePickerDialog mDialogMonthDayHourMinute;
    private TimePickerDialog mDialogHourMinute;
    private Text mTvTime;
    private Button btn_all;
    private Button btn_year_month_day;
    private Button btn_year_month;
    private Button btn_month_day_hour_minute;
    private Button btn_hour_minute;
    private SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public void onStart(Intent intent) {
        getWindow().setStatusBarColor(Color.getIntColor("#303f9f"));
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
    }

    private void initView() {
        btn_all = (Button) findComponentById(ResourceTable.Id_btn_all);
        btn_year_month_day = (Button) findComponentById(ResourceTable.Id_btn_year_month_day);
        btn_year_month = (Button) findComponentById(ResourceTable.Id_btn_year_month);
        btn_month_day_hour_minute = (Button) findComponentById(ResourceTable.Id_btn_month_day_hour_minute);
        btn_hour_minute = (Button) findComponentById(ResourceTable.Id_btn_hour_minute);
        mTvTime = (Text) findComponentById(ResourceTable.Id_tv_time);
        btn_all.setClickedListener(this);
        btn_year_month_day.setClickedListener(this);
        btn_year_month.setClickedListener(this);
        btn_month_day_hour_minute.setClickedListener(this);
        btn_hour_minute.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onDateSet(TimePickerDialog timePickerDialog, long millseconds) {
        String text = getDateToString(millseconds);
        mTvTime.setText(text);
    }

    public String getDateToString(long time) {
        Date date = new Date(time);
        return sf.format(date);
    }

    @Override
    public void onClick(Component component) {
        long tenYears = 10L * 365 * 1000 * 60 * 60 * 24L;
        int direction = getResourceManager().getConfiguration().direction;
        switch (component.getId()) {
            case ResourceTable.Id_btn_all:
                showDialogAll(tenYears, direction);
                break;
            case ResourceTable.Id_btn_year_month:
                showDialogYearMonth(direction);
                break;
            case ResourceTable.Id_btn_year_month_day:
                showDialogYearMonthDay(direction);
                break;
            case ResourceTable.Id_btn_month_day_hour_minute:
                showDialogMonthDayHourMinute(direction);
                break;
            case ResourceTable.Id_btn_hour_minute:
                showDialogHourMinute(direction);
                break;
            default:
                break;
        }
    }

    private void showDialogAll(long tenYears, int direction){
        mDialogAll = new TimePickerDialog.Builder()
                .setCallBack(this)
                .setCancelStringId("Cancel")
                .setSureStringId("Sure")
                .setTitleStringId("TimePicker")
                .setYearText("Year")
                .setMonthText("Month")
                .setDayText("Day")
                .setHourText("Hour")
                .setMinuteText("Minute")
                .setCyclic(false)
                .setOrientation(direction)
                .setMinMillseconds(System.currentTimeMillis())
                .setMaxMillseconds(System.currentTimeMillis() + tenYears)
                .setCurrentMillseconds(System.currentTimeMillis())
                .setThemeColor(Color.getIntColor("#80000000"))
                .setType(Type.ALL)
                .setWheelItemTextNormalColor(Color.getIntColor("#999999"))
                .setWheelItemTextSelectorColor(Color.getIntColor("#e60012"))
                .setWheelItemTextSize(12).build(getContext());
        mDialogAll.showDialog();
    }

    private void showDialogYearMonth(int direction){
        mDialogYearMonth = new TimePickerDialog.Builder()
                .setType(Type.YEAR_MONTH)
                .setThemeColor(Color.getIntColor("#3F51B5"))
                .setCallBack(this)
                .setOrientation(direction)
                .build(getContext());
        mDialogYearMonth.showDialog();
    }

    private void showDialogYearMonthDay(int direction){
        mDialogYearMonthDay = new TimePickerDialog.Builder()
                .setType(Type.YEAR_MONTH_DAY)
                .setCallBack(this)
                .setOrientation(direction)
                .build(getContext());
        mDialogYearMonthDay.showDialog();
    }

    private void showDialogMonthDayHourMinute(int direction){
        mDialogMonthDayHourMinute = new TimePickerDialog.Builder()
                .setType(Type.MONTH_DAY_HOUR_MIN)
                .setCallBack(this)
                .setOrientation(direction)
                .build(getContext());
        mDialogMonthDayHourMinute.showDialog();
    }

    private void showDialogHourMinute(int direction){
        mDialogHourMinute = new TimePickerDialog.Builder()
                .setType(Type.HOURS_MINS)
                .setCallBack(this)
                .setOrientation(direction)
                .build(getContext());
        mDialogHourMinute.showDialog();
    }
    
}
